package com.ylr.basic.framework.controller.api;

import com.ylr.base.common.tool.Result;
import com.ylr.basic.common.dto.EphemeralDataDto;
import com.ylr.basic.common.vo.ephemeral_data.EphemeralDataQueryVo;
import com.ylr.basic.common.vo.ephemeral_data.EphemeralDataVo;
import com.ylr.basic.framework.feign.YbEphemeralDataBaseClient;
import com.ylr.basic.framework.service.EphemeralDataService;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wyw
 * date: 2023-07-07 14:22:39
 * className: EphemeralDataApiController
 * version: 1.0
 * description:
 */
@RestController
@RequestMapping("/api/ephemeral-data")
@AllArgsConstructor
public class EphemeralDataApiController implements YbEphemeralDataBaseClient {

    private static final Logger log = LoggerFactory.getLogger(EphemeralDataApiController.class);

    private final EphemeralDataService ephemeralDataService;

    /**
     * 保存临时数据接口
     * @param ephemeralDataVo 参数对象
     * @return 结果
     */
    @PostMapping("/save")
    public Result<Object> saveEphemeralData(@Validated @RequestBody EphemeralDataVo ephemeralDataVo) {
        log.info("api => 开始保存临时数据操作。{}", ephemeralDataVo);

        ephemeralDataService.saveEphemeralData(ephemeralDataVo);

        log.info("api => 开始保存临时数据操作");
        return Result.success();
    }

    /**
     * 获取临时数据接口
     * @param businessId   业务主键
     * @param businessType 业务类型
     * @return 结果
     */
    @GetMapping("/get")
    public Result<EphemeralDataDto> getEphemeralData(@RequestParam("businessId") Long businessId,
                                                     @RequestParam("businessType") Integer businessType) {
        log.info("api => 开始获取临时数据操作。businessId={} businessType={}", businessId, businessType);

        EphemeralDataDto ephemeralDataDto = ephemeralDataService.getEphemeralData(businessId, businessType);

        log.info("api => 获取临时数据操作结束。{}", ephemeralDataDto);
        return Result.success(ephemeralDataDto);
    }

    /**
     * 批量获取临时数据接口
     * @param ephemeralDataQueryVo 参数对象
     * @return 结果
     */
    @PostMapping("/batch/get")
    public Result<List<EphemeralDataDto>> batchGetEphemeralData(@Validated @RequestBody EphemeralDataQueryVo ephemeralDataQueryVo) {
        log.info("api => 开始批量获取临时数据操作。{}", ephemeralDataQueryVo);

        List<EphemeralDataDto> map = ephemeralDataService.batchGetEphemeralData(ephemeralDataQueryVo);

        log.info("api => 批量获取临时数据操作结束。map={}", map);
        return Result.success();
    }

    /**
     * 删除临时数据接口
     * @param businessId   业务主键
     * @param businessType 业务类型
     * @return 结果
     */
    @DeleteMapping("/delete/v2")
    public Result<Object> deleteEphemeralData(@RequestParam("businessId") Long businessId,
                                              @RequestParam("businessType") Integer businessType) {
        log.info("api => 开始删除临时数据操作。businessId={} businessType={}", businessId, businessType);

        ephemeralDataService.deleteEphemeralData(businessId, businessType);

        log.info("api => 删除临时数据操作结束");
        return Result.success();
    }
}
